home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 160_01 / sources < prev    next >
C/C++ Source or Header  |  1985-11-24  |  35KB  |  1,745 lines

  1. ###asst.c
  2. /* EXAMPLES OF ASSIGNMENT
  3.  * (No output is produced)
  4.   */
  5.  main()
  6.      {
  7.      char c;
  8.      short i;
  9.  
  10.      c = 'A';
  11.     i = 65;
  12.     c = 'X';
  13.     i = -4;
  14.     }
  15. ###asst2.c
  16. /* asst2 - print assigned values
  17.  */
  18. main()
  19.     {
  20.     char c;
  21.     short i;
  22.  
  23.     c = 'A';
  24.     i = 65;
  25.     printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
  26.          c, c, c, c);
  27.     printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
  28.          i, i, i, i);
  29.     c = 'X';
  30.     i = -4;
  31.     printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
  32.          c, c, c, c);
  33.     printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
  34.          i, i, i, i);
  35.     }
  36. ###badpow.c
  37. /* badpow - demonstrate power function (argument mismatch error)
  38.  */
  39. #include "local.h"
  40. #include <math.h>
  41. main()
  42.     {
  43.     short i;
  44.  
  45.     for (i = 0; i < 10; ++i)
  46.         printf("2 to the power %d equals %.0f\n",
  47.             i, pow(2, i));
  48.     exit(SUCCEED);
  49.     }
  50. ###bdrill.c
  51. /* bdrill - binary arithmetic practice
  52.  */
  53. #include "local.h"
  54. #include <ctype.h>
  55. main()
  56.     {
  57.     short a, b;
  58.     short getbin();
  59.     void putbin();
  60.  
  61.     while ((a = getbin()) != 0 && (b = getbin()) != 0)
  62.         {
  63.         printf("\n    a = ");
  64.         putbin(a);
  65.         printf("\n    b = ");
  66.         putbin(b);
  67.         printf("\na + b = ");
  68.         putbin(a + b);
  69.         printf("\na & b = ");
  70.         putbin(a & b);
  71.         printf("\na | b = ");
  72.         putbin(a | b);
  73.         printf("\na ^ b = ");
  74.         putbin(a ^ b);
  75.         printf("\n");
  76.         }
  77.     }
  78. <:IT:>(<:fC:>getbin<:IT:> function goes here)<:fC:>
  79. <:IT:>(<:fC:>putbin<:IT:> function goes here)<:fC:>
  80. ###bits.c
  81. /* bits - examples of bitwise operations
  82.  */
  83. #include "local.h"
  84. main()
  85.     {
  86.     bits b1, b2;
  87.  
  88.     b1 = 0xF0F0 & 0x1234;
  89.     b2 = b1 | 0x60;
  90.     printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
  91.     b1 = ~1 & 0307;
  92.     b2 = (bits)b1 >> 2;
  93.     printf("b1=0%03o, b2=0%03o\n", b1, b2);
  94.     b1 = 0xF001 | 0x8801;
  95.     b2 = b1 & 0xB800;
  96.     printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
  97.     }
  98. ###blast.c
  99. /* blast - print countdown
  100.  */
  101. main()
  102.     {
  103.     short n;
  104.  
  105.     for (n = 10; n >= 0; n = n - 1)
  106.         printf("%d\n", n);
  107.     printf("Blast off!\n");
  108.     }
  109. ###blast2.c
  110. /* blast2 - print countdown
  111.  */
  112. main()
  113.     {
  114.     short n;
  115.  
  116.     for (n = 10; n >= 0; n = n - 1)
  117.         {
  118.         printf("%d\n", n);
  119.         if (n == 3)
  120.             printf("We have ignition!\n");
  121.         }
  122.     printf("Blast off!\n");
  123.     }
  124. ###cadd.c
  125. /* cadd - add two COMPLEX numbers
  126.  */
  127. #include "complex.h"
  128. COMPLEX cadd(x, y)
  129.     COMPLEX x, y;
  130.     {
  131.     COMPLEX z;
  132.  
  133.     z.real = x.real + y.real;
  134.     z.imag = x.imag + y.imag;
  135.     return (z);
  136.     }
  137. ###cap.c
  138. /* cap - capitalize initial letters
  139.  */
  140. #include "local.h"
  141. #include <ctype.h>
  142. main()
  143.     {
  144.     metachar c;
  145.  
  146.     while (isspace(c = getchar()))
  147.         putchar(c);
  148.     while (c != EOF)
  149.         {
  150.         putchar(toupper(c));
  151.         while (!isspace(c = getchar()))
  152.             putchar(c);
  153.         while (isspace(c))
  154.             {
  155.             putchar(c);
  156.             c = getchar();
  157.             }
  158.         }
  159.     }
  160. /* toupper - convert lower-case letter to lower case
  161.  */
  162. metachar toupper(c)
  163. metachar c;
  164.     {
  165.     return (islower(c) ? c + 'A' - 'a' : c);
  166.     }
  167. ###cap2.c
  168. /* cap - capitalize initial letters
  169.  */
  170. #include "local.h"
  171. #include <ctype.h>
  172. main()
  173.     {
  174.     tbool waswhite;    /* was preceding character a whitespace? */
  175.     metachar c;
  176.  
  177.     for (waswhite = YES; (c = getchar()) != EOF; waswhite = isspace(c))
  178.         {
  179.         if (!isspace(c) && waswhite)
  180.             putchar(toupper(c));
  181.         else
  182.             putchar(c);
  183.         }
  184.     }
  185. /* toupper - convert lower-case letter to lower case
  186.  */
  187. metachar toupper(c)
  188. metachar c;
  189.     {
  190.     return (islower(c) ? c + 'A' - 'a' : c);
  191.     }
  192. ###codes1.c
  193. /* codes1 - print ASCII codes
  194.  */
  195. main()
  196.     {
  197.     short c;
  198.  
  199.     for (c = 0; c <= 127; c = c + 1)
  200.         {
  201.         printf("%3d 0x%02x 0%03o", c, c, c);
  202.         if (' ' <= c && c <= '~')
  203.             printf(" '%c'", c);
  204.         if ('0' <= c && c <= '9')
  205.             printf(" digit");
  206.         if ('A' <= c && c <= 'Z')
  207.             printf(" uppercase");
  208.         if ('a' <= c && c <= 'z')
  209.             printf(" lowercase");
  210.         printf("\n");
  211.         }
  212.     }
  213. ###codes2.c
  214. /* codes2 - print ASCII codes
  215.  */
  216. #include "local.h"
  217. main()
  218.     {
  219.     short c;
  220.  
  221.     while ((c = getchar()) != EOF)
  222.         {
  223.         printf("%3d 0x%02x 0%03o", c, c, c);
  224.         if (' ' <= c && c <= '~')
  225.             printf(" '%c'", c);
  226.         if ('0' <= c && c <= '9')
  227.             printf(" digit");
  228.         if ('A' <= c && c <= 'Z')
  229.             printf(" uppercase");
  230.         if ('a' <= c && c <= 'z')
  231.             printf(" lowercase");
  232.         printf("\n");
  233.         }
  234.     }
  235. ###codes3.c
  236. /* codes3 - print ASCII codes
  237.  */
  238. #include "local.h"
  239. main()
  240.     {
  241.     metachar c;    /* return from getchar: char or EOF */
  242.  
  243.     while ((c = getchar()) != EOF)
  244.         {
  245.         printf("%3d 0x%02x 0%03o", c, c, c);
  246.         if (' ' <= c && c <= '~')
  247.             printf(" '%c'", c);
  248.         if ('0' <= c && c <= '9')
  249.             printf(" digit");
  250.         if ('A' <= c && c <= 'Z')
  251.             printf(" uppercase");
  252.         if ('a' <= c && c <= 'z')
  253.             printf(" lowercase");
  254.         printf("\n");
  255.         }
  256.     }
  257. ###codes4.c
  258. /* codes1 - print ASCII codes
  259.  */
  260. #include "local.h"
  261. #include <ctype.h>
  262. main()
  263.     {
  264.     metachar c;
  265.  
  266.     for (c = 0; c <= 127; c = c + 1)
  267.         {
  268.         printf("%3d 0x%02x 0%03o", c, c, c);
  269.         if (isprint(c))
  270.             printf(" '%c'", c);
  271.         if (isdigit(c))
  272.             printf(" D");
  273.         if (isupper(c))
  274.             printf(" UC");
  275.         if (islower(c))
  276.             printf(" LC");
  277.         if (isalpha(c))
  278.             printf(" L");
  279.         if (isalnum(c))
  280.             printf(" AN");
  281.         if (isspace(c))
  282.             printf(" S");
  283.         if (ispunct(c))
  284.             printf(" P");
  285.         if (iscntrl(c))
  286.             printf(" C");
  287.         printf("\n");
  288.         }
  289.     }
  290. ###collect.c
  291. #include <std.h>
  292. #define MAXL 512
  293.  
  294. /* collect line numbers on common index entries
  295.  */
  296. main(ac, av)
  297.     COUNT ac;
  298.     TEXT **av;
  299.         {
  300.     TEXT c, *s;
  301.     TEXT this[MAXL], next[MAXL];
  302.  
  303.     c = (ac <= 1) ? '\t' : av[1][0];
  304.     s = (ac <= 2) ? "\n\t" : av[2];
  305.     if (getfmt("%512p", this) <= 0)
  306.         this[0] = '\0';
  307.     putfmt("%p", this);
  308.     while (this[0])
  309.             {
  310.         if (getfmt("%512p", next) <= 0)
  311.             next[0] = '\0';
  312.         if (mthru(this, next, c))
  313.                 {
  314.             putfmt("%p", s);
  315.             putfmt("%p", suf(next, c));
  316.                 }
  317.         else
  318.                 {
  319.             putfmt("\n");
  320.             cpystr(this, next, NULL);
  321.             putfmt("%p", this);
  322.                 }
  323.             }
  324.         }
  325.  
  326. /* match strings thru null or c
  327.  */
  328. mthru(pa, pb, c)
  329.     TEXT *pa, *pb, c;
  330.         {
  331.     for ( ; *pa == *pb && *pa != c; pa++)
  332.         if (*pb == '\0')
  333.             return(1);
  334.         else
  335.             pb++;
  336.     return (*pa == *pb || *pa == '\0' && *pb == c);
  337.         }
  338.  
  339. /* locate suffix of s following c
  340.  */
  341. suf(s, c)
  342.     TEXT *s, c;
  343.         {
  344.     while (*s && *s++ != c)
  345.         ;
  346.     return (s);
  347.         }
  348. ###copy.c
  349. /* copy - copy input to output
  350.  */
  351. #include "local.h"
  352. main()
  353.     {
  354.     metachar c;
  355.  
  356.     while ((c = getchar()) != EOF)
  357.         putchar(c);
  358.     exit(SUCCEED);
  359.     }
  360. ###copy2.c
  361. /* copy2 - copy input to output 
  362.  */
  363. #include "local.h"
  364. main()
  365.     {
  366.     char s[BUFSIZ];
  367.  
  368.     while (getln(s, BUFSIZ) != EOF)
  369.         printf("%s", s);
  370.     }
  371. ###copy3.c
  372. /* copy3 - most efficient file copy
  373.  */
  374. #include "local.h"
  375. main()
  376.     {
  377.     char s[BUFSIZ];    /* array for characters */
  378.     short i;        /* number of characters read */
  379.  
  380.     while (0 != (i = read(STDIN, s, BUFSIZ)))
  381.         {
  382.         if (i < 0)
  383.             error("I/O error on read\n", "");
  384.         else if (i != write(STDOUT, s, i))
  385.             error("I/O error on write\n", "");
  386.         }
  387.     exit(SUCCEED);
  388.     }
  389. /* error - print fatal error message
  390.  */
  391. void error(s1, s2)
  392.     char s1[], s2[];
  393.     {
  394.     write(STDERR, s1, strlen(s1));
  395.     write(STDERR, " ", 1);
  396.     write(STDERR, s2, strlen(s2));
  397.     write(STDERR, "\n", 1);
  398.     exit(FAIL);
  399.     }
  400. ###dmpdem.c
  401. /* dump - print memory bytes
  402.  *        (Warning - some usages may be non-portable)
  403.  */
  404. #include "local.h"
  405. #define LINESIZE 16
  406. #define BYTEMASK 0xFF
  407. void dump(s, n)
  408.     char s[];        /* byte address to be dumped */
  409.     unsigned n;        /* number of bytes to dump */
  410.     {
  411.     unsigned i;        /* byte counter */
  412.  
  413.     for (i = 0; i < n; ++i)
  414.         {
  415.         if (i % LINESIZE == 0)
  416.             printf("\n%08x: ", &s[i]);
  417.         printf(" %02x", s[i] & BYTEMASK);
  418.         }
  419.     printf("\n");
  420.     }
  421. /* dmpdem - demonstrate dump function
  422.  */
  423. main()
  424.     {
  425.     char msg[16];
  426.     double d = 100.;
  427.  
  428.     strncpy(msg, "testing 1 2 3\n", sizeof(msg));
  429.  
  430.     /* case 1 - quite proper */
  431.     dump(msg, sizeof(msg));
  432.  
  433.     /* case 2 - ok, but output will vary with machine */
  434.     dump(&d, sizeof(d));
  435.  
  436.     /* case 3 - non-portable, may cause hardware error */
  437.     dump(0x40, 4);
  438.     exit(SUCCEED);
  439.     }
  440. ###dump.c
  441. /* dump - print memory bytes
  442.  *        (Warning - some usages may be non-portable)
  443.  */
  444. #include "local.h"
  445. #define LINESIZE 16
  446. void dump(s, n)
  447.     char s[];            /* byte address to be dumped */
  448.     unsigned n;            /* number of bytes to dump */
  449.     {
  450.     unsigned i;        /* byte counter */
  451.  
  452.     for (i =